package com.ruifu.proj.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.api.R;
import com.fhzncloud.cloud.common.security.util.SecurityUtils;
import com.ruifu.common.eneity.BaseEntityPlus;
import com.ruifu.common.po.Toolbar;
import com.ruifu.common.utils.BeanConverter;
import com.ruifu.proj.dto.toolbar.ToolbarDTO;
import com.ruifu.proj.service.ToolbarService;
import com.ruifu.proj.vo.toolbar.ToolbarVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author nyr
 * @create 2019/11/25
 */
@RestController
@CrossOrigin
@RequestMapping("/toolbar")
public class ToolbarController {
    @Autowired
    private ToolbarService toolbarService;

    /**
     * 查询可以再添加的菜单
     *
     * @param tlObjType 需要添加的菜单类型
     * @return
     */
    @GetMapping("/findList/{tlObjType}")
    public R findList(@PathVariable String tlObjType) {
        try {
            return toolbarService.findList(tlObjType);
        } catch (Exception e) {
            e.printStackTrace();
            return R.failed(e.getMessage());
        }
    }

    /**
     * 根据对象类型查询工具栏
     *
     * @param tlObjType 对象类型
     * @return
     */
    @GetMapping("/findByObjType/{tlObjType}")
    public R findByObjType(@PathVariable String tlObjType) {
        try {
            return R.ok(BeanConverter.copyList(toolbarService.list(Wrappers.<Toolbar>query()
                    .lambda()
                    .eq(Toolbar::getTlObjType, tlObjType)
                    .eq(BaseEntityPlus::getSetSid, SecurityUtils.getUser().getTenantId())
                    .orderByAsc(Toolbar::getTlPermissionCode)
            ), ToolbarVO.class));
        } catch (Exception e) {
            e.printStackTrace();
            return R.failed(e.getMessage());
        }
    }

    /**
     * 添加工具栏
     *
     * @param toolbarDTO
     * @return
     */
    @PostMapping("/save")
    public R save(@RequestBody ToolbarDTO toolbarDTO) {
        try {
            return R.ok(toolbarService.save(BeanConverter.copyProperties(toolbarDTO, Toolbar.class)));
        } catch (Exception e) {
            e.printStackTrace();
            return R.failed(e.getMessage());
        }
    }

    /**
     * 删除工具栏
     *
     * @param tlIdList 工具栏id数组
     * @return
     */
    @PostMapping("/batchDelete")
    public R batchDelete(@RequestBody List<Long> tlIdList) {
        try {
            return R.ok(toolbarService.removeByIds(tlIdList));
        } catch (Exception e) {
            e.printStackTrace();
            return R.failed(e.getMessage());
        }
    }

    /**
     * 修改菜单
     *
     * @param toolbarDTO
     * @return
     */
    @PostMapping("/update")
    public R update(@RequestBody ToolbarDTO toolbarDTO) {
        try {
            return R.ok(toolbarService.updateById(BeanConverter.copyProperties(toolbarDTO, Toolbar.class)));
        } catch (Exception e) {
            e.printStackTrace();
            return R.failed(e.getMessage());
        }
    }

    /**
     * 根据id查询菜单
     *
     * @param tlId 对象类型
     * @return
     */
    @GetMapping("/findByTlId/{tlId}")
    public R findByObjType(@PathVariable Long tlId) {
        try {
            return R.ok(BeanConverter.copyProperties(toolbarService.getById(tlId), ToolbarVO.class));
        } catch (Exception e) {
            e.printStackTrace();
            return R.failed(e.getMessage());
        }
    }
}
